package com.dr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dr.common.ValueUtil;
import com.dr.pojo.Dept;
import com.dr.pojo.Invoice;
import com.dr.pojo.Regist;
import com.dr.pojo.Registlevel;
import com.dr.pojo.Scheduling;
import com.dr.pojo.User;
import com.dr.service.DeptService;
import com.dr.service.InvoiceService;
import com.dr.service.RegistService;
import com.dr.service.SchedulingService;
import com.dr.service.registlevelService;


@Controller
@RequestMapping("/rg")
public class RegistCtrl {
	
	@Autowired
	private registlevelService levelservice;
	@Autowired
	private RegistService registService;
	@Autowired
	private InvoiceService invoiceservice;
	@Autowired
	private SchedulingService schedulingService;
	
	@RequestMapping("/regist")
	public String regist(Model md,Regist regist) {
		//发票号
		Invoice invoice =invoiceservice.nextInvoice(); 
		if(invoice !=null) {
			md.addAttribute("invoice", invoice);
		}else {
			md.addAttribute("invoice", new Invoice());
			md.addAttribute("errInvoice", "发票已用完，请重新申请");
		}
	
		//病历号
		String caseNumber =registService.nextCaseNumber();//病历号
		if(regist.getCaseNumber() == null) {
			md.addAttribute("caseNumber", caseNumber);//新的
		}else {
			md.addAttribute("caseNumber", regist.getCaseNumber());//旧的
		}
		
		//挂号级别
		List<Registlevel> levelList= levelservice.findRegistlevelList();
		if (levelList.size() != 0) {
			md.addAttribute("levelList", levelList);
		}else {
			md.addAttribute("levelList", new ArrayList<>());
			md.addAttribute("errRegistlevel", "获取挂号级别失败");
		}	
		md.addAttribute("regist", regist);
		return "regist/regist.html";
	}
//	查询病历信息
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md,String caseNumber) {
	//判断是否为空
		if(ValueUtil.isEmpty(caseNumber)) {
			caseNumber = "";
			}
		//根据病历号查询病历信息
			Regist regist = registService.findRegistByCaseNumber(caseNumber);
			if(regist == null) {
				regist =new Regist();
			}else {
				regist.setRegistLeID(0);  //查询病历不需要医生信息
				regist.setDeptName("");
				
			}
			
			md.addAttribute("regist", regist);
			System.out.println(regist);
			return regist(md, regist);
		
		}

	/*
	 * 查询排班医生
	 */
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md,Regist regist) {
		List<Scheduling> schedulingList=schedulingService.findSchedulingList(regist);
		md.addAttribute("schedulingList",schedulingList);
		
		//根据病历号，查询病历信息
		return regist(md,regist==null ? new Regist():regist);
	}

	/*
	 * 执行挂号
	 */
	@RequestMapping("/doRegist")
	public String doRegist(Regist regist,Model md,HttpSession session) {
		User loginUser = (User) session.getAttribute("loginUser");
		regist.setRegisterID(loginUser.getId());
		int count = registService.insertRegist(regist);
		if(count == 0) {
			md.addAttribute("errRegistLevel","挂号失败请联系系统管理员");
		}else {
			invoiceservice.updateInvoice(regist.getRegisterID(),regist.getInvoiceNum());
		}
		return "regist/article-list.html";
		
	}

}

